﻿@page "{id:int?}"
@model Scada.Web.Plugins.PlgWebPage.Areas.WebPage.Pages.LandingModel
@{
    Layout = "_BasicLayout";
}

@section Scripts {
    <script>
        var redirectUrl = "@Model.RedirectUrl";

        function replaceLocalhost(url) {
            let urlLo = url.toLowerCase();

            if (urlLo.startsWith("http://localhost") ||
                urlLo.startsWith("https://localhost") &&
                location.hostname.toLowerCase() !== "localhost") {

                let urlObj = new URL(url);
                urlObj.hostname = location.hostname;
                return urlObj.href;
            }

            return url;
        }

        $(document).ready(function () {
            const REDIRECT_DELAY = 500;

            let h = $(window).height();
            $("div.box-center").outerHeight(h);
            $("#iSpinner").removeClass("hidden"); // avoid flicker

            if (redirectUrl) {
                let url = replaceLocalhost(redirectUrl);
                console.log("Redirect to " + url)
                setTimeout(function () { location.href = url }, REDIRECT_DELAY);
            } else {
                console.error("Redirect URL is empty");
            }
        });
    </script>
}

@if (string.IsNullOrEmpty(@Model.ErrorMessage))
{
    <div class="box-center fs-2 text-info">
        <i id="iSpinner" class="fa-solid fa-spinner fa-spin-pulse fa-3x hidden"></i>
    </div>
}
else
{
    <div class="box-center fs-2 text-danger">
        @Model.ErrorMessage
    </div>
}
